package karaoke.controller;

import java.util.ArrayList;
import java.util.List;

import karaoke.excepcion.DAOExcepcion;
import karaoke.json.CustomGenericResponse;
import karaoke.json.CustomLocalResponse;
import karaoke.modelo.Local;
import karaoke.negocio.GestionLocal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("local")
public class LocalController {

	@RequestMapping(method = RequestMethod.GET)
    public String obtenerPaginaLocal() {
    	return "/mantenimiento/local";
	}
	
	
	@RequestMapping(value="/lista", method = RequestMethod.POST)
    public @ResponseBody CustomLocalResponse listaLocal(){
		 CustomLocalResponse res= new CustomLocalResponse();
		 GestionLocal gestion = new GestionLocal();
		 
		 try {
			List<Local> lstLocal= gestion.buscarPorNombre("");
			res.setRows(lstLocal);
			res.setRecords( String.valueOf(lstLocal.size()) );
			res.setPage( "1" );
        	
        	// Same. Assign a dummy total pages
			res.setTotal( "10" );
        	
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		return res; 
	 }
	
	@RequestMapping(value="/listaTodo", method = RequestMethod.GET)
    public @ResponseBody List<Local>  listaTodo(){
		 
		 GestionLocal gestion = new GestionLocal();
		 List<Local> lstLocal=new  ArrayList<Local>();
		 try {
			lstLocal= gestion.buscarPorNombre("");
        	
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			lstLocal= new ArrayList<Local>();
		}
		 
		return lstLocal; 
	 }
	
	
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody CustomGenericResponse add(Local local) {
    	
		
		GestionLocal gestion = new GestionLocal();
		 try {
			 
			 
				 
		local = gestion.insertar(local.getNombre(), local.getDireccion(), local.getTelefono(), local.getEmail(), local.getDescripcion(), local.getEstado(), local.getTrabajador().getIdTrabajador());
    	
    	// Check if successful
    	if ( local.getIdLocal()>0 ) {
    		// Success. Return a custom response
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(true);
			response.setMessage("Action successful!");
    		return response;
    		
    	} else {
    		// A failure. Return a custom response as well
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(false);
			response.setMessage("Action failure!");
    		return response;
    	}
		 } catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				CustomGenericResponse response = new CustomGenericResponse();
				response.setSuccess(false);
				response.setMessage("Action failure!");
	    		return response;
	    		// :(
				
			}
	}
	
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
    public @ResponseBody CustomGenericResponse edit(
    		@RequestParam("id") String id,
    		Local local) {
    	
		GestionLocal gestion = new GestionLocal();
		 try {
			 System.out.println("id"+ local.getIdLocal());
			 local.setIdLocal(Integer.valueOf(id));
			 local=gestion.actualizar(local.getIdLocal(),local.getNombre(),local.getDireccion(),local.getTelefono(),local.getEmail(),local.getDescripcion(), local.getEstado(),local.getTrabajador().getIdTrabajador());
    	
    	
    	// Check if successful
    	if ( local.getIdLocal()!=0 ) {
    		// Success. Return a custom response
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(true);
			response.setMessage("Action successful!");
    		return response;
    		
    	} else {
    		// A failure. Return a custom response as well
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(false);
			response.setMessage("Action failure!");
    		return response;
    	}
		 } catch (DAOExcepcion e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody CustomGenericResponse delete(
    		@RequestParam("id") String id
    ) {
    	
    	
    	GestionLocal gestion= new GestionLocal();
    	try{
    		gestion.eliminar(Integer.valueOf(id));
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(true);
			response.setMessage("Action successful!");
    		return response;
    		
	 } catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
    		CustomGenericResponse response = new CustomGenericResponse();
			response.setSuccess(false);
			response.setMessage("Action failure!");
    		return response;
		}
    		
    	
	}
	
}
